package cn.edu.zsc.rbac.view;

import cn.edu.zsc.rbac.domain.dto.UserDto;
import cn.edu.zsc.rbac.domain.vo.DeptVo;
import cn.edu.zsc.rbac.domain.vo.Result;
import cn.edu.zsc.rbac.domain.vo.RoleVo;
import cn.edu.zsc.rbac.domain.vo.UserVo;
import cn.edu.zsc.rbac.service.IUserService;
import cn.edu.zsc.rbac.service.array.UserService;

public class UserView extends BaseView<UserVo,IUserService>{
    //输出主界面菜单
    /**
     * 视图对象缓存数组
     */


    public static final String MENU = """
                -------------
                   用户管理
                -------------
                | 0 返回上级  |
                | 1 用户列表  |
                | 2 创建用户  |
                | 3 更新用户  |
                | 4 切换状态  |
                -------------
                请输入选项："""
            ;



    private final DeptView deptView;
    private final RoleView roleView;

    private UserView(){
        deptView =DeptView.getInstance();
        roleView =RoleView.getInstance();
        service = UserService.getInstance();
    }

    private static UserView singleton;


    public static UserView getInstance(){
        if(singleton==null){
            singleton = new UserView();
        }
        return singleton;
    }

    public  void show() {
        String menuChoice;
        do {
            System.out.println(MENU);
            menuChoice = MainView.input.nextLine();
            switch (menuChoice) {
                case "0" -> System.out.println("已返回上级菜单！");
                case "1" -> list();
                case "2" -> create();
                case "3" -> update();
                case "4" -> toggle();
                default -> System.out.print("输入非法，请输入正确的菜单编号！");
            }
        } while (!"0".equals(menuChoice));

    }


    public  void list(){
        Result result = service.list();
        cache=(UserVo[])result.data;
        System.out.println("ID\t用户名\t姓名\t手机\t邮箱\t部门名称\t角色名称\t状态");
        for (UserVo user : cache) {
            System.out.printf("%d\t%s\t%s\t%s\t%s\t%s\t%s\n",user.id,user.name,user.mobile,user.email,user.deptName,user.roleName,
                    user.enabled?"启用":"禁用");
        }
    }
    public  void create(){
        System.out.print("请输入用户名：");
        String username = MainView.input.nextLine();
        System.out.print("请输入姓名：");
        String name = MainView.input.nextLine();
        System.out.print("请输入手机号：");
        String mobile = MainView.input.nextLine();
        System.out.print("请输入邮箱：");
        String email = MainView.input.nextLine();

        System.out.print("请输入部门ID：");
        int deptId = 0;

        do {
            if(deptView.cache==null||deptView.cache.length==0){
                printError("系统还没有创建任何部门信息，请先创建部门信息。");
                return;
            }
            deptView.list();
            DeptVo dept = deptView.getCacheById();
            if(dept!=null){
                deptId = dept.id;
            }
        }while(deptId==0);

        System.out.print("请输入角色ID：");
        int roleId = 0;

        do {
            if(roleView.cache==null||roleView.cache.length==0){
                printError("系统还没有创建任何角色信息，请先创建角色信息。");
                return;
            }
            roleView.list();
            RoleVo role = roleView.getCacheById();
            if(role!=null){
                roleId = role.id;
            }
        }while(roleId==0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = service.create(dto);

        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }

    public  void update(){
        UserVo  user = getCacheById();
        if(user==null){
            return;
        }

        System.out.print("请输入用户名，按回车表示不修改：");
        String username = MainView.input.nextLine();
        username="".equals(username)?user.username:username;

        System.out.print("请输入姓名，按回车表示不修改：");
        String name = MainView.input.nextLine();
        name="".equals(name)?user.name:name;

        System.out.print("请输入手机号码，按回车表示不修改：");
        String mobile = MainView.input.nextLine();
        mobile="".equals(mobile)?user.mobile:mobile;

        System.out.print("请输入邮箱，按回车表示不修改：");
        String email = MainView.input.nextLine();
        email="".equals(email)?user.email:email;

        System.out.print("请输入部门ID：");
        int deptId = 0;

        do {
            if(deptView.cache==null||deptView.cache.length==0){
                printError("系统还没有创建任何部门信息，请先创建部门信息。");
                return;
            }
            deptView.list();
            DeptVo dept = deptView.getCacheById();
            if(dept!=null){
                deptId = dept.id;
            }
        }while(deptId==0);

        System.out.print("请输入角色ID：");
        int roleId = 0;

        do {
            if(roleView.cache==null||roleView.cache.length==0){
                printError("系统还没有创建任何角色信息，请先创建角色信息。");
                return;
            }
            roleView.list();
            RoleVo role = roleView.getCacheById();
            if(role!=null){
                roleId = role.id;
            }
        }while(roleId==0);

        UserDto dto = new UserDto(username, name, mobile, email, deptId, roleId);
        Result result = service.update(dto,user.id);
        if(!result.success){
            System.out.println(result.message);
            return;
        }

        System.out.println(result.message);
        list();
    }

    public  void toggle(){
        UserVo  user = getCacheById();
        if(user==null){
            return;
        }
        if(user.enabled){
            System.out.print("输入'Y'确认禁用，输入其他则取消禁用：");
        }else{
            System.out.print("输入'Y'确认启用，输入其他则取消启用：");
        }
        String confirm = MainView.input.nextLine();
        if(!"Y".equalsIgnoreCase(confirm)){
            System.out.println("您已取消修改状态的操作");
            return;
        }
        Result result = service.toggle(user.id);
        if(!result.success){
            printError(result.message);
            return;
        }
        System.out.println(result.message);
        list();
    }


}
